package com.huike.contract.service.impl;

import com.huike.business.mapper.TbBusinessMapper;
import com.huike.clues.domain.TbAssignRecord;
import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbRulePool;
import com.huike.clues.mapper.SysUserMapper;
import com.huike.clues.mapper.TbAssignRecordMapper;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.clues.service.ISysUserService;
import com.huike.clues.service.ITbRulePoolService;
import com.huike.clues.utils.HuiKeCrmDateUtils;
import com.huike.clues.utils.JobUtils;
import com.huike.common.constant.Constants;
import com.huike.common.core.domain.entity.SysUser;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import com.huike.common.utils.StringUtils;
import com.huike.common.utils.bean.BeanUtils;
import com.huike.contract.domain.vo.TransferVo;
import com.huike.contract.service.ITransferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class TransferServiceImpl implements ITransferService {

    @Autowired
    private ISysUserService userService;

    @Autowired
    private TbAssignRecordMapper assignRecordMapper;

    @Autowired
    private ITbRulePoolService rulePoolService;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private TbClueMapper clueMapper;


    @Autowired
    private TbBusinessMapper businessMapper;

    /**
     *
     * @param user 用户信息
     * @return 转派信息列表
     */
    @Override
    public List<TransferVo> selectTransferList(SysUser user) {
        //复制数据
        TbAssignRecord tbAssignRecord = new TbAssignRecord();
        BeanUtils.copyBeanProp(tbAssignRecord,user);
        //查询分配表获取集合数据
        List<TbAssignRecord> recordList = assignRecordMapper.selectAssignRecordList(tbAssignRecord);
        //获取userId集合
        HashSet<Long> set = new HashSet<>();
        for (TbAssignRecord record : recordList) {
            set.add(record.getUserId());
        }
        List<TransferVo> list = new ArrayList<>();
        for (Long userId : set) {
            TransferVo transferVo = new TransferVo();
            //查询线索数量
            int clueNum = assignRecordMapper.countAssignCluesByUser(userId);
            //查询商机数量
            int businessNum = assignRecordMapper.countAssignBusinessByUser(userId);
            //查询user信息
            SysUser sysUser = sysUserMapper.selectUserById(userId);
            //组装返回对象数据
            transferVo.setUserId(userId);
            transferVo.setClueNum(clueNum);
            transferVo.setBusinessNum(businessNum);
            transferVo.setUserName(sysUser.getUserName());
            transferVo.setPhonenumber(sysUser.getPhonenumber());
            transferVo.setCreateTime(sysUser.getCreateTime());
            list.add(transferVo);
        }
        return list;
    }

    /**
     * 转派线索或商机
     * @param type 转派类型 0线索 1商机
     * @param userId 转出id
     * @param transferUserId 转入id
     */
    @Override
    public void addTransfer(String type, Long userId, Long transferUserId) {
        //把某个用户的所有线索或商机转给另外一个人
        TbAssignRecord tbAssignRecord = new TbAssignRecord();
        tbAssignRecord.setUserId(userId);
        tbAssignRecord.setType(type);
        tbAssignRecord.setLatest("1");
        //根据userId查询tb_assign_record分配记录表的信息
        List<TbAssignRecord> tbAssignRecords = assignRecordMapper.selectAssignRecordList(tbAssignRecord);
        for (TbAssignRecord record : tbAssignRecords) {
            //copy信息并修改
            BeanUtils.copyBeanProp(tbAssignRecord,record);
            //查询转入user信息,并组装新的分配表信息
            SysUser user = sysUserMapper.selectUserById(transferUserId);
            tbAssignRecord.setUserId(transferUserId);
            tbAssignRecord.setUserName(user.getUserName());
            tbAssignRecord.setDeptId(user.getDeptId());
            tbAssignRecord.setCreateTime(DateUtils.getNowDate());
            tbAssignRecord.setCreateBy(SecurityUtils.getUsername());
            //修改转出user的分配记录表的latest为0
            assignRecordMapper.updateLatest(record.getAssignId(),type);
            //添加tb_assign_record分配记录表
            assignRecordMapper.insertAssignRecord(tbAssignRecord);
            //修改线索表或商机表的转派状态
            if ("0".equals(type)) {
                //修改线索表的转派和状态为“1”
                clueMapper.setTransfer(record.getAssignId(),"1");
            } else if ("1".equals(type)) {
                //修改商机表的转派状态
                businessMapper.setTransfer(record.getAssignId(),"1");
            }
        }
    }
}
